package de.gameduell.framework.facebook {
	import fl.transitions.TweenEvent;
	import fl.transitions.easing.Regular;

	import de.gameduell.framework.asset.AssetEvent;
	import de.gameduell.framework.debug.Debug;
	import de.gameduell.framework.facebook.reporting.FBBaseTracker;
	import de.gameduell.framework.gui.animation.GDTween;
	import de.gameduell.framework.gui.wrapper.SpriteWrapper;
	import de.gameduell.framework.locale.LocaleData;
	import de.gameduell.framework.util.Library;

	import flash.display.Sprite;
	import flash.events.Event;

	public class Page extends SpriteWrapper implements IPage {
		
		protected var _page:Sprite;
		protected var basePageManager:FBBasePageManager;
		protected var controller:IFBBaseController;
		protected var library:Library;
		protected var _isActive:Boolean;
		protected var _isContent:Boolean;
		protected var localeData:LocaleData;
		protected var _autoShow:Boolean;
		protected var _id:String;
		protected var _pageSettings:PageSettings;
		protected var tracker:FBBaseTracker;

		public function Page(aPageManager:FBBasePageManager, anId:String){
			super(new Sprite());
			_id = anId;
			library     = Library.getInstance();
			localeData  = LocaleData.getInstance();
			basePageManager = aPageManager;
			tracker = FBBaseTracker.getInstance();
			controller  = basePageManager.getController();
			controller.addEventListener(AssetEvent.COMPLETE, onAssetLoaded);

			_pageSettings = controller.getPageSettingsById(_id);			
			if(_pageSettings != null){
				position   = _pageSettings.position;
				_autoShow  = _pageSettings.autoShow;
				_isContent = _pageSettings.isContent;
			}else{
				Debug.trace("Page::Page: no settings for " + _id + " available", Debug.WARNING);
			}
		}
		
		protected function onAssetLoaded(event:AssetEvent):void {
			if(event.asset.id == _id) {
				controller.removeEventListener(AssetEvent.COMPLETE, onAssetLoaded);
				page = library.createSprite(event.asset.id);
				initPage();
				show();
			}
		}
		
		protected function initPage():void {
			
		}

		public function get page():Sprite {
			return _page;
		}
		
		public function set page(aPage:Sprite):void {
			_page = aPage;
		}
		
		public function show(event:Event = null):void {
//			Debug.trace("Page::show: id -> " + _id);
			if(page != null){
				page.alpha = 0;
				sprite.addChild(page);
				fadeIn();
			}else{
				controller.loadAsset(_id);
			}
		}
		
		public function hide(event:Event = null):void {
			if(page != null){
				active = false;
				var myTween:GDTween = new GDTween(page, "alpha", Regular.easeIn, page.alpha, 0, 0.2, true);
				myTween.addEventListener(TweenEvent.MOTION_FINISH, remove);
				myTween.start();
			}
		}
		
		protected function remove(event:Event = null):void {
			try{
				sprite.removeChild(_page);
			}catch(e:Error){
				Debug.trace("Page::remove: " + e, Debug.WARNING);
			}
		}
		
		public function set active(isActive:Boolean):void {
			_isActive = isActive;
			sprite.mouseEnabled  = _isActive;
			sprite.mouseChildren = _isActive;
		}
		
		public function fadeOut(event:Event =  null):void {
			var myTween:GDTween = new GDTween(page, "alpha", Regular.easeIn, page.alpha, 0, 0.3, true);
			myTween.start();
		}
		
		public function fadeIn(event:Event =  null):void {
			var myTween:GDTween = new GDTween(page, "alpha", Regular.easeIn, 0, 1, 0.3, true);
			myTween.addEventListener(TweenEvent.MOTION_FINISH, enabledPage);
			myTween.start();
		}
		
		protected function enabledPage(event:TweenEvent):void {
			active = true;
		}
		
		public function set isContent(isContentPage:Boolean):void {
			_isContent = isContentPage;
		}
		
		public function get isContent():Boolean {
			return _isContent;
		}
		
		public function get id():String {
			return _id;
		}
		
		public function get showInstant():Boolean {
			return _autoShow;
		}
		
		public function get active():Boolean {
			return _isActive;
		}
	}
}
